import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:znmm/config/app_color.dart';
import 'package:znmm/config/app_gaps.dart';
import '../mag/edti_temp_mag.dart';
import 'auto_temp_btn.dart';

//自动生成
class AutoTextDialog extends StatefulWidget {
  const AutoTextDialog({super.key});

  @override
  State<AutoTextDialog> createState() => _AutoTextDialogState();
}

class _AutoTextDialogState extends State<AutoTextDialog> {
  final TextEditingController _autoController = TextEditingController();

  String _autoTextStr = "";

  @override
  void initState() {
    super.initState();
    //autoController.text = "baidu";
  }

  @override
  void dispose() {
    _autoController.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: Get.width * 0.8,
        height: Get.height * 0.7,
        decoration: BoxDecoration(
          color: AppColor.white,
          borderRadius: BorderRadius.circular(10),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ///关键字输入框
            Container(
              height: 30.h,
              padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 5.h),
              margin: EdgeInsets.symmetric(horizontal: 10.w, vertical: 5.h),
              decoration: BoxDecoration(
                color: AppColor.greyD8D8D8,
                borderRadius: BorderRadius.circular(5),
              ),
              child: TextField(
                controller: _autoController,
                decoration: InputDecoration(
                  hintText: '请输入关键字',
                  contentPadding: EdgeInsets.only(
                    bottom: 13.h,
                  ),
                  hintStyle: TextStyle(
                    fontSize: 14.sp,
                  ),
                  border: InputBorder.none,
                  suffixIcon: GestureDetector(
                    onTap: () {
                      //生成
                      EdtiTempMag().autoTemplateData(
                        _autoController.text,
                        callback: (p0) {
                          if (mounted) {
                            setState(() {
                              _autoTextStr = p0;
                            });
                          }
                        },
                      );
                    },
                    child: Container(
                      width: 50.w,
                      height: 30.h,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: AppColor.blue396EC6,
                        borderRadius: BorderRadius.circular(5),
                      ),
                      child: Text(
                        '生成',
                        style: TextStyle(
                          fontSize: 12.sp,
                          color: AppColor.white,
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
            AppGaps.vGaps10w,
            Expanded(
              child: Container(
                width: double.infinity,
                padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 5.h),
                margin: EdgeInsets.symmetric(horizontal: 10.w, vertical: 5.h),
                decoration: BoxDecoration(
                  color: AppColor.greyD8D8D8,
                  border: Border.all(
                    color: AppColor.grey525357,
                  ),
                  borderRadius: BorderRadius.circular(5),
                ),
                child: SingleChildScrollView(
                  child: Text(
                    _autoTextStr,
                    style: TextStyle(
                      fontSize: 14.sp,
                    ),
                  ),
                ),
              ),
            ),
            Row(
              children: [
                GptButton(
                  text: '插入',
                  isLeft: true,
                  color: AppColor.blue396EC6,
                  onTap: () {
                    EdtiTempMag().insertText(_autoTextStr);
                    Get.back();
                  },
                ),
                GptButton(
                  text: '清除',
                  isLeft: false,
                  color: AppColor.orangeE8A849,
                  onTap: () {
                    setState(() {
                      _autoTextStr = "";
                    });
                  },
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}
